<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-card class="box-card">
      <el-row>
        <el-col :span="12">
          <div class="grid-content bg-purple search">
            <el-form ref="query" :model="query" label-width="100px">
              <el-form-item style="margin-bottom:0px" label="名称">
                <i slot="label" class="el-icon-search" style="font-size:20px"></i>
                <el-input v-model="query.value" placeholder="请输入文章名称"></el-input>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple-light" style="text-align:right">
            <el-button type="primary" @click="queryAsk">查询</el-button>
            <el-button @click="queryReset">重置</el-button>
          </div>
        </el-col>
      </el-row>
    </el-card>

    <!-- 表格 -->
    <el-card class="box-card">
      <el-button type="primary" style="margin-bottom:20px" @click="addList">添加</el-button>
      <el-table
        :data="tableData"
        :header-cell-style="{background:'#eee'}"
        border
        v-loading="loading"
        style="width: 100%;margin-bottom: 20px;"
      >
        <el-table-column align="center" type="index" width="50" label="序号"></el-table-column>
        <el-table-column align="center" prop="name" label="名称"></el-table-column>
        <el-table-column align="center" prop="add_time" label="创建时间"></el-table-column>
        <el-table-column align="center" prop="update_time" label="更新时间"></el-table-column>
        <el-table-column align="center" fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button slot="reference" type="text" size="small" @click="handleDetail(scope.row)">详情</el-button>
            <el-button
              style="margin-left:10px"
              @click="handleEdite(scope.row)"
              type="text"
              size="small"
            >编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="text-align:right">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="currentPage"
          :total="total"
          @current-change="currentChange"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { richtextList } from '@/api/system'
import { parseTime } from '@/utils/index'

export default {
  name: 'RichtextList',
  data() {
    return {
      // 分页数据
      total: 0,
      currentPage: 1,
      pageSize: 10,
      // 表数据
      tableData: [],
      //input
      query: {
        value: ''
      },
      loading: false
    }
  },
  mounted() {
    this.getRichtextList()
  },
  methods: {
    async getRichtextList() {
      this.loading = true
      const res = await richtextList({
        page: this.currentPage,
        limit: this.pageSize,
        keywords: this.query.value
      })
      this.loading = false
      console.log(res)
      this.total = res.count
      this.tableData = res.data
      this.tableData.forEach((item) => {
        if (item.add_time == 0) {
          item.add_time = '-'
        } else {
          item.add_time = parseTime(item.add_time, '{y}-{m}-{d} {h}:{i}:{s}')
        }
        if (item.update_time == 0) {
          item.update_time = '-'
        } else {
          item.update_time = parseTime(
            item.update_time,
            '{y}-{m}-{d} {h}:{i}:{s}'
          )
        }
      })
    },
    //筛选
    queryAsk() {
      this.currentPage = 1
      this.getRichtextList()
    },
    queryReset() {
      this.total = 0
      this.pageSize = 10
      this.query.value = ''
      this.currentPage = 1
      this.resetForm('query')
      this.getRichtextList()
    },
    //分页改变
    currentChange(current) {
      this.currentPage = current
      this.getRichtextList()
    },
    addList() {
      this.$router.push({
        name: 'RichtextInfo',
        params: { oprate: '添加' }
      })
    },
    handleDetail(row) {
      this.$router.push({
        name: 'RichtextInfo',
        params: { id: row.id, oprate: '详情' }
      })
    },
    handleEdite(row) {
      this.$router.push({
        name: 'RichtextInfo',
        params: { id: row.id, oprate: '编辑' }
      })
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.drawer = false
    }
  }
}
</script>

<style lang="scss" scoped>
.box-card {
  margin-bottom: 20px;
}
.search {
  display: flex;
  align-items: center;
}
::v-deep .el-input--medium .el-input__inner {
  width: 360px;
}
::v-deep .el-drawer.rtl {
  width: 650px !important;
  overflow: auto;
}
</style>